package com.byzx.wine.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginAction {
	/**
	 * 登陆
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login")
	public ModelAndView login(HttpServletResponse response, HttpServletRequest request,HttpSession session) {
		String name = request.getParameter("loginname");
		String psw = request.getParameter("password");
		UsernamePasswordToken token = new UsernamePasswordToken(name, psw);
//		session.setAttribute("username", name);
		Subject currentUser = SecurityUtils.getSubject();
//		currentUser.getSession();	
		try {
			currentUser.login(token);
			if (currentUser.isAuthenticated()) {
				session.setAttribute("username", name);
				return  new ModelAndView("common/main");	
			}else{
				return new ModelAndView("common/login");	
			}
		} catch (Exception e) {
			System.out.println("登陆失败"+e.getMessage());
			return new ModelAndView("common/login");
		}
	}
	@RequestMapping(value = "/tologin")
	public String tologin(HttpServletResponse response,
			HttpServletRequest request) {
		return "common/login";
	}
}